package com.wendy.common.core.json.jackson;

import com.wendy.common.core.constant.BaseDbType;
import com.wendy.common.core.constant.BaseType;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * 金额转换
 * @Author: ALEX WANG
 * @Date: 2020/9/22 10:25
 */
public  class MoneyYuanToCents implements BaseDbType<Long>, BaseType<String> {
    private static final Long bs=100L;

    public String yuan;

    public Long cents;

    public MoneyYuanToCents() {
    }

    @Override
    public Long selectDbTypeValue() {
        return cents;
    }

    @Override
    public void addDbTypeValue(Long cents) {
        this.cents=cents;
        if(cents !=null){
            DecimalFormat sdf = new DecimalFormat("#.##");
            this.yuan= sdf.format(new BigDecimal(cents).divide(BigDecimal.valueOf(bs),2, RoundingMode.DOWN));
        }
    }


    @Override
    public String selectTypeValue() {
        return yuan;
    }

    @Override
    public void addTypeValue(String yuan) {
        this.yuan=yuan;
        if(StringUtils.isNotBlank(yuan)){
            this.cents= new BigDecimal(yuan).multiply(BigDecimal.valueOf(bs)).longValue();
        }
    }
}
